/*
 * QTeXPreview
 * Copyright (C) 2008, Philip Pronin (philip.pronin@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef RENDER_H
#define RENDER_H

#include <QImage>
#include <QPixmap>
#include <QWidget>

QT_FORWARD_DECLARE_CLASS(TeXFrontEnd)

class RenderAreaWidget: public QWidget {
	Q_OBJECT
	Q_PROPERTY(bool vertical READ isVertical WRITE setVertical)
	Q_PROPERTY(RenderState renderState READ getRenderState)
	Q_ENUMS(RenderState)

public:
	RenderAreaWidget(QWidget* parent = NULL);
	virtual~ RenderAreaWidget() {}

	QSize sizeHint() const {return minimumSizeHint();}
	QSize minimumSizeHint() const;

	bool isVertical() const {return vertical;}
	void setVertical(bool v) {vertical = v;}

	enum RenderState { Rendering, Waiting };

	RenderState getRenderState() const { return renderState; }

	const QPixmap& getPixmap() const;
	QImage getImage() const;

	static QImage prepareImage(const QPixmap& pixmap);

signals:
	void renderStateChanged(RenderAreaWidget::RenderState state);

public slots:
	void setTeX(const QString& equation);

protected:
	void paintEvent(QPaintEvent* event);
	void mousePressEvent(QMouseEvent* event);
	void mouseMoveEvent(QMouseEvent* event);

private:
	void setRenderState(RenderState state);

private slots:
	void renderStarted();
	void renderFinished(QByteArray data);

private:
	TeXFrontEnd* frontEnd;
	QPixmap image;
	bool vertical;
	RenderState renderState;
	QString eq;
	QPoint dragPosition;
	bool dragImageMode;
};

#endif /* RENDER_H */
